package org.suwenqian.airoleplayingchatbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.suwenqian.airoleplayingchatbackend.common.exception.ServiceException;
import org.suwenqian.airoleplayingchatbackend.dao.dto.req.RoleInsertReqDTO;
import org.suwenqian.airoleplayingchatbackend.dao.dto.req.RoleListReqDTO;
import org.suwenqian.airoleplayingchatbackend.dao.dto.resp.RoleListRespDTO;
import org.suwenqian.airoleplayingchatbackend.dao.entity.RoleDO;
import org.suwenqian.airoleplayingchatbackend.dao.mapper.RoleMapper;
import org.suwenqian.airoleplayingchatbackend.service.RoleService;


@Service
@RequiredArgsConstructor
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleDO> implements RoleService {

    private final RoleMapper roleMapper;

    @Override
    public void addOrUpdateRole(RoleInsertReqDTO requestParm) {
        //1. 判断是否为默认角色
        LambdaQueryWrapper<RoleDO> eq = Wrappers.lambdaQuery(RoleDO.class)
                .eq(RoleDO::getName, requestParm.getName());
        RoleDO roleDOEntity = roleMapper.selectOne(eq);
        if (roleDOEntity != null){
            if (roleDOEntity.getIsDefault() == 1){
                throw new ServiceException("暂不支持修改默认角色，可尝试新增想聊天的角色");
            }
        }
        String promptTemplate = "你需要根据以下任务中的描述进行角色扮演，你只能以设定的角色身份回答，不是用户身份或AI身份，如记错身份，你将受到惩罚， " +
                "首先请记住你的名字，你现在的名字是" + requestParm.getName() + ";" + requestParm.getDescription() + "这是对你的简要描述，你具备以下能力"
                + requestParm.getAbility() + ";" + "其次你的性格特点是" + requestParm.getPersonalities() + ";" + "最后你所在的背景以及你的简要故事是"
                + requestParm.getBackgroundStory() + ";" + "以上就是你的现在所扮演的角色设定，请你务必记住自己的身份并根据角色设定与用户进行交互";

        RoleDO roleDO = RoleDO.builder()
                .name(requestParm.getName())
                .description(requestParm.getDescription())
                .avatarUrl(requestParm.getAvatarUrl())
                .promptTemplate(promptTemplate)
                .helloMsg(requestParm.getHelloMsg())
                .ability(requestParm.getAbility())
                .backgroundStory(requestParm.getBackgroundStory())
                .personalities(requestParm.getPersonalities())
                .isPublic(requestParm.getIsPublic())
                .isDefault(0)
                .build();
        roleMapper.insertOrUpdate(roleDO);
    }

    @Override
    public void deleteRole(String id) {
        roleMapper.deleteById(id);
    }

    @Override
    public IPage<RoleListRespDTO> queryRoleList(RoleListReqDTO requestParm) {
        Page<RoleDO> page = new Page<>(requestParm.getCurrent(), requestParm.getSize());
        // 返回的角色列表为：默认角色 + 用户创建的角色
        LambdaQueryWrapper<RoleDO> queryWrapper = Wrappers.lambdaQuery(RoleDO.class)
                .eq(RoleDO::getUserId, requestParm.getUserId())
                .or()
                .eq(RoleDO::getIsDefault, 1);
        IPage<RoleDO> resultList = roleMapper.selectPage(page, queryWrapper);
        return resultList.convert(each -> BeanUtil.toBean(each, RoleListRespDTO.class));
    }

    @Override
    public IPage<RoleListRespDTO> searchRoleList(String requestParm) {
        //1. 查找背景，角色设定，塑造角色性格和对话风格的Prompt模板中有关键词的角色
        Page<RoleDO> page = new Page<>(1, 10);
        LambdaQueryWrapper<RoleDO> queryWrapper = Wrappers.lambdaQuery(RoleDO.class)
                .like(RoleDO::getDescription, requestParm)
                .or()
                .like(RoleDO::getPromptTemplate, requestParm);
        IPage<RoleDO> resultList = roleMapper.selectPage(page, queryWrapper);
        return resultList.convert(each -> BeanUtil.toBean(each, RoleListRespDTO.class));
    }
}
